﻿#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>

struct A
{
	int _a : 2;
	int _b : 5;
	int _c : 10;
	int _d : 30;
};
//int main()
//{
//	struct A sa = { 0 };
//	scanf("%d", &sa._b);//这是错误的 
//
//	//正确的⽰范 
//	int b = 0;
//	scanf("%d", &b);
//	sa._b = b;
//	return 0;
//}


//struct S
//{
//	char a : 3;
//	char b : 4;
//	char c : 5;
//	char d : 4;
//};
//struct S s = { 0 };
//s.a = 10;
//s.b = 12;
//s.c = 3;
//s.d = 4;


struct A
{
	int _a : 2;
	int _b : 5;
	int _c : 10;
	int _d : 30;
};

struct S
{
	int data[1000];
	int num;
};
struct S s = { {1,2,3,4}, 1000 };
//结构体传参 
void print1(struct S s)
{
	printf("%d\n", s.num);
}
//结构体地址传参 
void print2(struct S* ps)
{
	printf("%d\n", ps->num);
}
int main()
{
	print1(s); //传结构体 
	print2(&s); //传地址 
	return 0;
}


#pragma pack(1)//设置默认对齐数为1 
//struct S
//{
//	char c1;
//	int i;
//	char c2;
//};
#pragma pack()//取消设置的对齐数，还原为默认 

int main4()
{
	printf("%d\n", sizeof(struct S));
	return 0;
}


//练习1 
struct S1
{
	char c1; //0~1
	int i; //4~8
	char c2; //8~9

	//最后到9，但是整体大小是最大对齐数的整数倍，所以是12
};

//练习2 
struct S2
{
	char c1; //0~1
	char c2; //1~2
	int i; //4~8
};

//练习3 
struct S3
{
	double d; //第一个变量从0开始，0~8
	char c; //8~9
	int i; //12~16
};

//练习4-结构体嵌套问题 
struct S4
{
	char c1; //0~1
	struct S3 s3; //8~24
	double d; //24~32
};


int main3()
{
	printf("练习一：%d\n", sizeof(struct S1));
	printf("练习二：%d\n", sizeof(struct S2));
	printf("练习三：%d\n", sizeof(struct S3));
	printf("练习四：%d\n", sizeof(struct S4));
	return 0;
}

struct Stu
{
	char name[20];//名字
	int age;//年龄
	char sex[5];//性别
	char id[20];//学号
};

struct Node
{
	int data;
	struct Node* next;
};


int main2()
{
	//按照结构体成员的顺序初始化 
	struct Stu s = { "张三", 20, "男", "20230818001" };
	printf("name: %s\n", s.name);
	printf("age : %d\n", s.age);
	printf("sex : %s\n", s.sex);
	printf("id : %s\n", s.id);

	//按照指定的顺序初始化 
	struct Stu s2 = { .age = 18, .name = "lisi", .id = "20230818002", .sex = "⼥" };
	printf("name: %s\n", s2.name);
	printf("age : %d\n", s2.age);
	printf("sex : %s\n", s2.sex);
	printf("id : %s\n", s2.id);
	return 0;
}


int main1()
{
	struct Stu s1;//创建结构体变量

	return 0;
}
